package com.wn55.hrms.web.controller;


import com.github.pagehelper.PageInfo;

import com.wn55.hrms.entity.Role;
import com.wn55.hrms.service.RoleService;
import com.wn55.hrms.service.vo.RoleVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author z
 */
@RestController
@RequestMapping("role")
@Api(value = "此接口用于角色管理")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 分页条件查询角色
     *
     * @param now 第几页
     * @param size 每页条数
     * @param name  姓名
     * @return
     */
    @GetMapping("/roleList")
    @ApiOperation(value = "条件分页查询角色")
    @Cacheable(value = "rolesData",keyGenerator = "keyGenerator")
    public ResultResponse<PageInfo<RoleVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            String name){
        ResultResponse<PageInfo<RoleVo>> result = null;
//        System.out.println("收到角色"+now+size+name);

        try {
            PageInfo<RoleVo> pageInfo = roleService.roleList(now,size,name);
            result = new ResultResponse<>(200,"查询角色成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询角色失败!");
        }
        return result ;
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping("/modify")
    @ApiOperation(value = "修改角色")

    public ResultResponse<Void> modify(@RequestBody Role role){
        ResultResponse<Void> result = null;

        try {
            role.setUpdateTime(new Date());
            roleService.modify(role);
            result = new ResultResponse<>(200,"更新角色成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"更新角色失败!");
        }
        return result ;
    }

    /**
     * 修改权限
     * @param role
     * @return
     */
    @PutMapping("/rolePermission")
    @ApiOperation(value = "根据角色修改权限")

    public ResultResponse<Void> rolePermission(@RequestBody RoleVo role){
        ResultResponse<Void> result = null;

        try {
            roleService.rolePermission(role);
            result = new ResultResponse<>(200,"更新权限成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"更新权限失败!");
        }
        return result ;
    }

    /**
     * 查询全部角色
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部角色")
    @Cacheable(value = "rolesAllData",keyGenerator = "keyGenerator")
    public ResultResponse<List<Role>> getAll(){
        ResultResponse<List<Role>> result = null;

        try {
            List<Role> list = roleService.queryAll();
            result = new ResultResponse<>(200,"查询全部角色成功!",list);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询全部角色失败!");
        }
        return result ;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加角色")

    public ResultResponse<Void> add(@RequestBody Role role){
        ResultResponse<Void> result = null;

        try {
            role.setInsertTime(new Date());
            role.setUpdateTime(new Date());
            roleService.add(role);
            result = new ResultResponse<>(200,"添加角色成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加角色失败!");
        }
        return result ;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除角色")

    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            roleService.removeBykey(id);
            result = new ResultResponse<>(200,"删除角色成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除角色失败!");
        }
        return result ;
    }
}
